import smtplib

# 邮件库
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# from_email, e_key, tomail, title, content

# 邮件类
class EmailTool:
    # 邮件发送方法 发送激活链接或者随机激活码
    def sendmail(self, from_email, e_key, tomail, title, content):
        try:
            # 设置邮件主体
            msg = MIMEMultipart()
            # 设置标题
            msg['Subject'] = title
            # 发件人
            msg['From'] = from_email
            # 收件人
            msg['To'] = tomail

            # 设置邮件文本
            part = MIMEText('内容：%s' % content, 'html', 'utf-8')
            msg.attach(part)

            # 登录QQ邮箱
            s = smtplib.SMTP_SSL('smtp.qq.com', 465)
            s.login(from_email, e_key)
            # 调用发送方法
            s.sendmail(from_email, tomail, msg.as_string())
            # 关闭邮件链接
            s.close()
            return True
        except Exception as e:
            print("错误信息", e)
            return False


if __name__ == '__main__':
    et = EmailTool()